using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Utbildningsmaterial
{
    class BlackAndWhiteComponent : DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        ResolveTexture2D resolveTarget;
        Effect blackAndWhite;

        public BlackAndWhiteComponent(Game game)
            : base(game)
        {

        }
        
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            blackAndWhite = Game.Content.Load<Effect>("BlackAndWhite");

            PresentationParameters pp = GraphicsDevice.PresentationParameters;
            resolveTarget = new ResolveTexture2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1, pp.BackBufferFormat);
        }

        protected override void UnloadContent()
        {
            resolveTarget.Dispose();
        }

        public override void Draw(GameTime gameTime)
        {
            if (Enabled)
            {
                Game.GraphicsDevice.ResolveBackBuffer(resolveTarget);

                spriteBatch.Begin(SpriteBlendMode.None,
                                  SpriteSortMode.Immediate,
                                  SaveStateMode.None);


                blackAndWhite.Begin();
                blackAndWhite.CurrentTechnique.Passes[0].Begin();

                spriteBatch.Draw(resolveTarget, Vector2.Zero, Color.White);

                blackAndWhite.CurrentTechnique.Passes[0].End();
                blackAndWhite.End();

                spriteBatch.End();
                
                Game.GraphicsDevice.RenderState.DepthBufferEnable = true;
                Game.GraphicsDevice.RenderState.AlphaBlendEnable = false;
                Game.GraphicsDevice.RenderState.AlphaTestEnable = false;
            }
        }
    }
}
